﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Globalization;

/// <summary>
/// Summary description for Converters
/// </summary>
public static class Converters
{
    public static Guid SafeConvertToGuid(object guid)
    {
        try
        {
            if (guid == null)
                return Guid.Empty;
            else
                return (Guid)guid;
        }
        catch (Exception)
        {
            Guid rzlt;
            string guidString = guid.ToString();
            if (Guid.TryParse(guidString, out rzlt))
                return rzlt;
            else
                return Guid.Empty;
        }
    }

    public static int SafeConvertToInt(object intValue, int undefinedValue = -1)
    {
        try
        {
            if (intValue == null)
                return undefinedValue;
            else
                return (int)intValue;
        }
        catch (Exception)
        {
            string tmp = intValue.ToString();
            int rzlt;
            if (!Int32.TryParse(tmp, out rzlt))
                rzlt = undefinedValue;
            return rzlt;
        }
    }

    public static decimal SafeConvertToDecimal(object decimalValue)
    {
        try
        {
            if (decimalValue == null)
                return 0.00m;
            else
                return (decimal)decimalValue;
        }
        catch (Exception)
        {
            NumberStyles style = NumberStyles.AllowDecimalPoint;
            CultureInfo culture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
            string tmp = decimalValue.ToString().Trim().Replace(" ", "").Replace(",", ".");

            double rzlt;
            if (!Double.TryParse(tmp, style, culture, out rzlt))
                rzlt = 0.00;
            return (decimal)rzlt;
        }
    }

    public static long SafeConvertToLong(object intValue, long? defaultValue = -1L)
    {
        try
        {
            if (intValue == null)
                return defaultValue ?? -1L;
            else
                return (long)intValue;
        }
        catch (Exception)
        {
            string tmp = intValue.ToString();
            long rzlt;
            if (!Int64.TryParse(tmp, out rzlt))
                rzlt = defaultValue ?? -1L;
            return rzlt;
        }
    }

    public static DateTime SafeConvertToDate(object dateValue, DateTime? defaultValue = null)
    {
        DateTime dfValue = defaultValue ?? DateTime.Now;
        try
        {
            if (dateValue == null)
                return dfValue;
            else
                return (DateTime)dateValue;
        }
        catch (Exception)
        {
            string tmp = dateValue.ToString();
            DateTime rzlt;
            if (!DateTime.TryParse(tmp, out rzlt))
                rzlt = dfValue;
            return rzlt;
        }
    }

    public static DateTime RemoveHours(DateTime date)
    {
        int year = date.Year;
        int month = date.Month;
        int day = date.Day;
        return new DateTime(year, month, day);
    }

    public static string ConvertMpsLoginType(this string loginType)
    {
        switch (loginType)
        {
            case "namePassword":
                return "passwd";
            case "clientCard":
                return "idm";
            case "barCode":
                return "barCd";
            case "cardNumber":
                return "card#";
            default:
                return loginType;
        }
    }
}